using System;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Silverlight.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace IsoStorageFacade_WP7_Tests
{
    [TestClass]
    public class Test_Sys_IO_PSDirectoryInfo
    {
        private const string RootFolder = @"\Isolated\";
        private const string TestLine1 = "this is a test";
        private const string TestLine2 = "Last Line";
        private string TestDir = "TestDir";
        private const string TestFile = "test.txt";
        private const string kTextReaderTestFile = "textReaderTest.txt";

        private static string[] GetRootDirNames()
        {
            return PSDirectory.GetDirectories(RootFolder);
        }

        [TestInitialize]
        public void setup()
        {
            PSDirectory.SetCurrentDirectory(@"\");
            // create file
            using (var strm = PSFile.Create(RootFolder + TestFile))
            {
                strm.WriteByte(65);
                strm.Flush();
                strm.Close();
            }
            PSDirectory.CreateDirectory(RootFolder + TestDir);
        }

        [TestCleanup]
        public void teardown()
        {
            PSDirectory.SetCurrentDirectory(@"\");
            PSFile.Delete(RootFolder + TestFile);
            PSDirectory.Delete(RootFolder + TestDir);
        }

        [TestMethod]
        public void Get_Dirs_Should_Get_At_Least_One_When_PSDirInfo_Is_Root()
        {
            //Add1DirInRoot();
            try
            {
                var di = new PSDirectoryInfo(@"\");
                var names = di.GetDirectories();
                Assert.IsNotNull(names, "GetDirectories should not be null");
                Assert.IsTrue(names.Length > 0, "GetDirectories should return more than 1 dir");

                PSDirectory.SetCurrentDirectory(TestDir);
                names = di.GetDirectories();
                Assert.IsNotNull(names, "GetDirectories should not be null (and SetCurDir shouldn't affect it)");
                Assert.IsTrue(names.Length > 0, "GetDirectories should return more than 1 dir (and SetCurDir shouldn't affect it)");
            }
            finally
            {
                PSDirectory.SetCurrentDirectory(@"\");
            }
        }

        [TestMethod]
        public void Get_Dir_Names_Should_Get_No_Records_When_PSDirInfo_Is_Test_SubDir()
        {
            //Add1DirInRoot();
            try
            {
                var di = new PSDirectoryInfo(RootFolder + TestDir);
                var names = di.GetDirectories();
                Assert.IsNotNull(names, "GetDirectories should not be null");
                Assert.IsTrue(names.Length == 0, "GetDirectories should return no values (our dir should be empty)");

                PSDirectory.SetCurrentDirectory(RootFolder + TestDir);
                names = di.GetDirectories();
                Assert.IsNotNull(names, "GetDirectories should not be null (CurrentDir shouldn't affect this)");
                Assert.IsTrue(names.Length == 0, "GetDirectories should return no values (CurrentDir shouldn't affect this)");
            }
            finally
            {
                PSDirectory.SetCurrentDirectory(@"\");
            }
        }

        [TestMethod]
        public void Creating_A_Directory_Should_Result_In_Directory_Created()
        {
            try
            {
                var newDir = new PSDirectoryInfo(RootFolder + "zzzTest1");
                newDir.Create();
                Assert.IsNotNull(newDir, "result should not be null");
                Assert.IsTrue(newDir.Name.Length > 1, "result name should not be empty");
                Assert.IsTrue(newDir.Name.Contains("zzzTest1"), "result name Should contain test1 directory");

                var names = IsolatedStorageFile.GetUserStoreForApplication().GetDirectoryNames(RootFolder);
                Assert.IsNotNull(names, "GetDirectories should not be null");
                Assert.IsTrue(names.Length > 1, "GetDirectories should return more than 1 value");

                PSDirectory.SetCurrentDirectory(RootFolder + @"zzzTest1\");
                newDir = new PSDirectoryInfo("zzzTest2");
                newDir.Create();
                Assert.IsNotNull(newDir, "result should not be null");
                Assert.IsTrue(newDir.Name.Length > 1, "result name should not be empty");
                Assert.IsTrue(newDir.Name.Contains("zzzTest2"), "result name Should contain test1 directory");

                PSDirectory.SetCurrentDirectory(@"\");
                names = PSDirectory.GetDirectories(RootFolder, "zzzTest2");
                Assert.IsNotNull(names, "GetDirectories should not be null");
                Assert.IsTrue(names.Length == 0, "GetDirectories should not find our dir which should have been created in a subdir (thanks to a set current dir)");

                names = PSDirectory.GetDirectories(RootFolder + @"zzzTest1\", @"zzzTest2");
                Assert.IsNotNull(names, "GetDirectories should not be null");
                Assert.IsTrue(names.Length == 1, "GetDirectories should find our dir which was created in this Sub dir been created in a subdir (thanks to a set current dir)");
            }
            finally
            {
                PSDirectory.SetCurrentDirectory(@"\");
                if (PSDirectory.Exists(RootFolder + @"zzzTest1\zzzTest2"))
                    PSDirectory.Delete(RootFolder + @"zzzTest1\zzzTest2");
                if (PSDirectory.Exists(RootFolder + "zzzTest1"))
                    PSDirectory.Delete(RootFolder + "zzzTest1");
            }
        }

        [TestMethod]
        public void Deleting_A_Directory_Should_Result_In_Directory_Removed()
        {
            try
            {
                PSDirectoryInfo createdDir = PSDirectory.CreateDirectory(RootFolder + "zzzTest1");

                var names1 = GetRootDirNames();
                Assert.IsNotNull(names1, "GetDirectories should not be null");
                Assert.IsTrue(names1.Length > 1, "GetDirectories should return more than 1 value");

                Assert.IsTrue(PSDirectory.Exists(createdDir.FullName), "Created Dir should exist before deletion via the API");

                createdDir.Delete();

                var names = GetRootDirNames();
                Assert.IsNotNull(names, "GetDirectories should not be null");
                Assert.IsTrue(names.Length > 0, "GetDirectories should return at least 1 value (our default)");
                Assert.IsFalse(names.Where(name => name.Contains("zzzTest1")).Count() > 0, "GetDirectories should not contain our test1 directory");
                Assert.IsFalse(names.Contains("zzzTest1"), "GetDirectories Should Not contain Test1 directory");
            }
            finally
            {
                if (IsolatedStorageFile.GetUserStoreForApplication().DirectoryExists(RootFolder + "zzzTest1"))
                {
                    IsolatedStorageFile.GetUserStoreForApplication().DeleteDirectory(RootFolder + "zzzTest1");
                }
            }
        }

        [TestMethod]
        public void Exists_Should_Return_True_When_A_Folder_Exists_And_False_When_It_Doesnt()
        {
            try
            {
                Assert.IsTrue((new PSDirectoryInfo(RootFolder + TestDir)).Exists, "test folder should exist");
                Assert.IsFalse((new PSDirectoryInfo(RootFolder + "zzzzzz" + TestDir)).Exists, "non existent folder should not exist");
            }
            finally
            {
            }
        }
    }
}